package com.geektime.congestion.wallet.domain;


import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.Date;

@NoArgsConstructor
@Accessors(chain = true)
@Getter
public class VirtualWallet {

    private String uuid;

    private BigDecimal balance;

    private Date createTime;

    private Date updateTime;

    public VirtualWallet(String uuid, Date createTime) {
        this.uuid = uuid;
        this.balance = BigDecimal.ZERO;
        this.createTime = createTime;
        this.updateTime = createTime;
    }

    /**
     * 充值
     *
     * @param amount 增加金额
     */
    public void add(BigDecimal amount) {
        if (amount == null || BigDecimal.ZERO.compareTo(amount) >= 0) {
            throw new IllegalArgumentException("amount should be positive");
        }
        this.balance = this.balance.add(amount);
        this.updateTime = new Date();
    }

    /**
     * 提现
     *
     * @param amount 提现金额
     */
    public void subtract(BigDecimal amount) {
        if (amount == null || BigDecimal.ZERO.compareTo(amount) >= 0) {
            throw new IllegalArgumentException("amount should be positive");
        }
        if (this.balance.compareTo(amount) < 0) {
            throw new IllegalArgumentException("balance is not enough");
        }
        this.balance = this.balance.subtract(amount);
        this.updateTime = new Date();
    }

    @Override
    public String toString() {
        return "VirtualWallet{" +
                "uuid='" + uuid + '\'' +
                ", balance=" + balance +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }
}
